﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Roman Ranzmaier (http://www.ranzmaier.at/contact.aspx).  All rights reserved.
// 
// Codeplex project: http://gadgeteernetworking.codeplex.com
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using Ranzmaier.NETMF.GadgeteerNetworking;
using System.Net;

namespace Sample2
{
    public partial class Program
    {


        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {           
            Debug.Print("Program Started");

            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);

            ethernet.UseStaticIP("10.0.0.225", "255.255.255.0", "10.0.0.138");
            var s = ethernet.CreateUDPServer(5001);
            s.DataReceived += new UdpServer.DataReceivedEventHandler(s_DataReceived);
            s.Start();
            


        }

        void s_DataReceived(EndPoint sender, UdpServer.DataReceiveEventArgs e)
        {
            Debug.Print(e.Data.ToString());
        }

        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            // Send Data to local UDPServer
            byte[] x = new byte[8];
            x[0] = 0x01;

            using (UdpClient c = new UdpClient())
                c.Send(x, x.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("10.0.0.225"), 5001));
        }
    }
}
